qui(
    function (require, exports, module) {
        Broadcast.Grid = function (options) {
            this.dao = Broadcast.Dao;
            this.task = Broadcast.Task;
            this.config = Broadcast.Config;
            this.options = options || {};
        };
        Com.inherits(Broadcast.Grid, Ancestor.Grid).extend({
            //===========config manager start ========================/
            runConfig: function () {
                var channelId = Com.url.getParameter("id");
                if (channelId == null || isNaN(channelId)) {
                    EpgWeb.Plugin.showTips({content: "need channelId", type: "error"})
                    return;
                }
                var self = this;
                self.$grid = $('#jqGridTable');
                self.dao.channelId = channelId;
                var taskArr = [];
                taskArr.push(self.task.getPlayPlatform(self.dao.cfgName.PlayPlatform));
                taskArr.push(self.task.getPlatSettings(self.dao.cfgName.platSettings, self.dao.channelId));

                $.each(taskArr, function (index, element) {
                    EpgWeb.ajaxQueue.push(element.regName, element.options);
                })
                EpgWeb.ajaxQueue.afterLoadComplete(function () {
                    self.startConfig();
                })
                EpgWeb.ajaxQueue.startLoad();
            },
            startConfig: function () {
                var self = this;
                self.initJqGrid();
                self.initConfigForm();
            },
            initConfigForm: function () {
                var self = this;
                $("input[name^='onlineKey'][value='-1']").prop("checked", "checked");
                $("input[name^='regionKey'][value='-1']").prop("checked", "checked");
                $("input[name='checkCopyRightType'][value='-1']").prop("checked", "checked");
                $("input[name='checkBossType'][value='-1']").prop("checked", "checked");
                $("input[name='checkPublishType'][value='-1']").prop("checked", "checked");
                $("input[name=submitBtn]").bind("click", function (event) {
                    self.submitConfigForm();
                })
                var entity = self.dao.getPlatSettings().data

                $("input[name='checkCopyRightType'][value='" + entity.checkCopyRightType + "']").prop("checked", "checked");
                $("input[name='checkBossType'][value='" + entity.checkBossType + "']").prop("checked", "checked");
                $("input[name='checkPublishType'][value='" + entity.checkPublishType + "']").prop("checked", "checked");

                $.each(entity.regionLimitArr, function (i, element) {
                    $("input[name='regionKey_" + element + "'][value=1]").prop("checked", "checked");
                })
                $.each(entity.onlineLimitArr, function (i, element) {
                    $("input[name='onlineKey_" + element + "'][value=1]").prop("checked", "checked");
                })
                console.log("initForm end");
            },
            initJqGrid: function () {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TABLE_CONFIG);
                self.$grid.jqGrid(gridConfig);
                var responseData = self.dao.getPlatforms();
                var createDate = responseData.data;
                var fillData = {"total": 1, "page": 1, "records": createDate.length, "rows": createDate};
                self.$grid[0].addJSONData(fillData);
            },
            submitConfigForm: function () {
                var self = this;
                var entity = self.dao.createEntity();
                var onlineArr = $("input[name^='onlineKey'][value='1']:checked");
                var regionArr = $("input[name^='regionKey'][value='1']:checked");
                entity.checkCopyRightType = $("input[name='checkCopyRightType']:checked").val();
                entity.checkBossType = $("input[name='checkBossType']:checked").val();
                entity.checkPublishType = $("input[name='checkPublishType']:checked").val();
                var clientKey = "";
                $.each(onlineArr, function (i, element) {
                    clientKey = element.name.replace("onlineKey_", "")
                    entity.onlineLimitArr.push(parseInt(clientKey));
                })
                $.each(regionArr, function (i, element) {
                    clientKey = element.name.replace("regionKey_", "")
                    entity.regionLimitArr.push(parseInt(clientKey));
                })

                self.dao.doAuthData({
                    url: '/epg/config_manager.html',
                    doError:function(){
                        SystemDialog.queryAlert("很抱歉，您没有相关权限!");
                    },
                    doSuccess:function(){
                        if (EpgWeb.ajaxQueue.isRunning()) {
                            EpgWeb.Plugin.showTips({
                                content: "数据处理中，请稍后操作"
                            })
                            return;
                        }
                        var msg = self.checkDiff(entity, self.dao.getPlatSettings().data);
                        if (msg.length > 0) {
                            SystemDialog.queryConfirm(msg.join("<br>"), function (flag) {
                                if (flag === true) {
                                    self.dao.savePlatSettings( {channelId: self.dao.channelId, configData: JSON.stringify(entity)} ,function(data,submitEntity){
                                        self.settingCallBack(data,submitEntity);
                                    })
                                }
                            })
                        } else {
                            self.dao.savePlatSettings({channelId: self.dao.channelId, configData: JSON.stringify(entity)},function(data,submitEntity){
                                self.settingCallBack(data,submitEntity);
                            })
                        }
                    }
                })
            },
            settingCallBack: function (data, submitEntity) {
                var self = this;
                if (data != null) {
                    SystemDialog.queryAlert(data.msg);
                    self.dao.getPlatSettings().data = submitEntity;
                    console.log(data.code);
                } else {
                    SystemDialog.queryAlert("保存数据失败")
                    console.log(JSON.stringify(data));
                }
            },
            checkDiff: function (entity, origin) {
                var self = this;
                //权限配置
                var msg = [];
                /*
                 勾选付费：勾选后付费节目可添加至轮播台，是否确认修改？
                 勾选版权
                 勾选未发布：勾选后未发布节目可添加至轮播台，是否确认修改？
                 端选项发生变化：勾选后*端未上线的节目 *端有地域播控的节目可添加至轮播台，是否确认修改？
                 * */
                //    var origin = Broadcast.Dao,
                var typeMsg = [];
                if (entity.checkBossType == 1 && origin.checkBossType != entity.checkBossType) {
                    typeMsg.push(self.config.ERROR_MSG.bossAlertMsg)
                }
                if (entity.checkPublishType == -1 && origin.checkPublishType != entity.checkPublishType) {
                    typeMsg.push(self.config.ERROR_MSG.publishAlertMsg)
                }

                if (typeMsg.length > 0) {
                    msg.push(typeMsg.join(" "));
                }
                var responseData = self.dao.getPlatforms();
                var qiyiOnline = false;
                var ppsOnline = false;
                var qiyiRegion = false;
                var ppsRegion = false;
                $.each(responseData.data, function (index, element) {
                    //   clientArr =  [element.descr ] ;
                    if (entity.onlineLimitArr.indexOf(parseInt(element.key)) === -1 && origin.onlineLimitArr.indexOf(parseInt(element.key)) !== -1) {
                        //      clientArr.push(  Broadcast.memo.clientAlertMsg1)
                        if (element.val.indexOf("PPS") != -1) {
                            ppsOnline = true;
                        } else {
                            qiyiOnline = true;
                        }

                    }
                    if (entity.regionLimitArr.indexOf(parseInt(element.key)) === -1 && origin.regionLimitArr.indexOf(parseInt(element.key)) !== -1) {
                        //       clientArr.push(  Broadcast.memo.clientAlertMsg2)
                        if (element.val.indexOf("PPS") != -1) {
                            ppsRegion = true;
                        } else {
                            qiyiRegion = true;
                        }
                    }
                });
                //播控管理
                var clientControlArr = [];
                if (qiyiOnline || qiyiRegion) {
                    var clientArr = ["IQIYI平台"];
                    if (qiyiOnline) {
                        clientArr.push(self.config.ERROR_MSG.clientAlertMsg1)
                    }
                    if (qiyiRegion) {
                        clientArr.push(self.config.ERROR_MSG.clientAlertMsg2)
                    }
                    clientControlArr.push(clientArr.join(""))
                }
                if (ppsOnline || ppsRegion) {
                    var clientArr = ["PPS平台"];
                    if (ppsOnline) {
                        clientArr.push(self.config.ERROR_MSG.clientAlertMsg1)
                    }
                    if (ppsRegion) {
                        clientArr.push(self.config.ERROR_MSG.clientAlertMsg2)
                    }
                    clientControlArr.push(clientArr.join(""))
                }
                if (clientControlArr.length > 0) {
                    msg = msg.concat(clientControlArr);
                }
                if (msg.length > 0) {
                    msg.push("可添加至轮播台,是否确认修改?")
                }
                return msg;
            }
            //===========config manager end ========================/

        })
    }
);